<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加部门</title>
    <link rel="stylesheet" th:href="@{lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="hidden" name="parentId" th:value="${parentId}">
    <div class="layui-form-item">
        <label class="layui-form-label required">部门名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="部门名称不能为空" placeholder="请输入部门名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序号</label>
        <div class="layui-input-block">
            <input type="number" name="sort" placeholder="请输入排序号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" lay-skin="switch" lay-text="正常|禁用" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">类型</label>
        <div th:if="${type eq 1}">
            <input type="radio" name="type" value="1" title="实验室" checked>
        </div>
        <div th:if="${type eq 2}">
            <input type="radio" name="type" value="2" title="小组" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data.field);

            var datas = data.field;
            if (datas.status == 'on'){
                datas.status=0;//正常状态
            }else {
                datas.status=1;
            }

            //向后台发送数据并进行添加操作
            $.ajax({
                url:"dept/addDeptSubmit",
                type:"POST",
                data:datas,
                success:function (result) {
                    if (result.code==200){
                        layer.msg("添加成功",{
                            icon:6,
                            time:500,
                        },function () {
                                parent.window.location.reload();//重新页面
                                var ifremeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(ifremeIndex);
                        })
                    }else{
                        layer.msg("添加失败");
                    }
                }
            })
            /*var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {

                // 关闭弹出层
                layer.close(index);

                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

            });*/

            return false;
        });

    });
</script>
</body>
</html>